package com.huixue.cn.ztej.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TTudentInfo;
import com.huixue.cn.ztej.dao.TTudentInfoMapper;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TTudentService {

	private static Logger logger = LoggerFactory.getLogger(TTudentService.class);
	
	@Autowired
	private TTudentInfoMapper tTudentInfoMapper;
	
	public void save(TTudentInfo info){
		tTudentInfoMapper.insertSelective(info);
	}
	
	@Transactional(readOnly = false)
	public void update(TTudentInfo info){
		tTudentInfoMapper.updateByPrimaryKeySelective(info);
	}
	
	public void updateStudent(TTudentInfo info){
		tTudentInfoMapper.updateByPrimaryKeySelective(info);
	}
	
	public void delete(Integer id){
		tTudentInfoMapper.deleteByPrimaryKey(id);
	}
	
	public TTudentInfo getInfo(Integer id){
		return tTudentInfoMapper.selectByPrimaryKey(id);
	}

	public TTudentInfo findbyOpenId(String openId){
		return tTudentInfoMapper.findbyOpenId(openId);
	}
	
	public Results searchByPage(Map<String,Object> searchParams,Integer pageNumber, Integer pageSize) {
		PageParameter page = new PageParameter();
		page.setCurrentPage(pageNumber);
		page.setPageSize(pageSize);
		searchParams.put("page", page);
		List<Map<String, Object>> list = tTudentInfoMapper.searchByPage(searchParams);
		Map<String, Object> rmap = new HashMap<String, Object>();
		rmap.put("total", page.getTotalPage());
		rmap.put("page", pageNumber);						//当前页
		rmap.put("records", page.getTotalCount());			//总记录数
		rmap.put("pageSize", pageSize);						//每页个数
		rmap.put("rows", list);
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc(),rmap);
		return results;
	}
}
